package com.example.expand_list_view.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.expand_list_view.R;
import com.example.expand_list_view.beans.School;
import com.example.expand_list_view.beans.Student;

import java.util.ArrayList;
import java.util.List;

public class MyExpandListAdapter extends BaseExpandableListAdapter {

    private List<School> groups;
    private List<ArrayList<Student>> childData;

    private Context context;

    public MyExpandListAdapter(List<School> groups, List<ArrayList<Student>> childData, Context context) {
        this.groups = groups;
        this.childData = childData;
        this.context = context;
    }

    @Override
    public int getGroupCount() {
        return groups.size();
    }

    //某个分组下面的child的数量
    @Override
    public int getChildrenCount(int groupPosition) {
        return childData.get(groupPosition).size();
    }

    @Override
    public School getGroup(int groupPosition) {
        return groups.get(groupPosition);
    }

    @Override
    public Student getChild(int groupPosition, int childIndex) {
        return childData.get(groupPosition).get(childIndex);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childIndex) {
        return childIndex;
    }

    //这个方法不知道是干嘛的
    @Override
    public boolean hasStableIds() {
        return false;
    }

    //取得用于显示给定分组的视图，进返回分组的视图对象

    /**
     * @param i      当前加载到哪个数据了
     * @param b      是否展开了
     * @param view   数据绑定到哪个视图
     * @param parent
     * @return
     */
    @Override
    public View getGroupView(int i, boolean b, View view, ViewGroup parent) {
        GroupViewHolder groupViewHolder;
        if (view == null) { //如果数据还没有绑定视图，找到视图绑定
            view = LayoutInflater.from(context).inflate(R.layout.list_group_item, parent, false);
            groupViewHolder = new GroupViewHolder();
            groupViewHolder.textView = (TextView) view.findViewById(R.id.tv_group_name);
            //把viewholder交给view进行缓存
            view.setTag(groupViewHolder);
        } else {
            //如果视图不是空，拿到绑定的viewholder
            groupViewHolder = (GroupViewHolder) view.getTag();
        }
        groupViewHolder.textView.setText(groups.get(i).getName());

        return view;
    }


    //取得用于显示给定分组的视图，仅返回child的视图对象
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean b, View view, ViewGroup viewGroup) {
        ChildViewHolder childViewHolder;
        //数据还没绑定视图
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.list_child_item, viewGroup, false);
            childViewHolder = new ChildViewHolder();
            childViewHolder.imageView = view.findViewById(R.id.img_icon);
            childViewHolder.textView = view.findViewById(R.id.tv_name);
            //交给view进行缓存
            view.setTag(childViewHolder);
        } else {
            //groupPosition这个数据 如果已经绑定了视图，从视图中拿到viewHolder
            childViewHolder = (ChildViewHolder) view.getTag();
        }
        //childViewHolder.imageView.setImageResource(); 不设置图片了，使用布局文件中默认的图片
        childViewHolder.textView.setText(childData.get(groupPosition).get(childPosition).getName());

        return view;
    }

    //设置子列表是否可以选中 ,如果设置为false ，childItem的点击事件无效
    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }

    //groupItem的布局缓存
    class GroupViewHolder {
        private TextView textView;
    }

    //childItem的布局缓存
    class ChildViewHolder {
        private ImageView imageView;

        private TextView textView;
    }
}
